#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTime>
#include <QFileDialog>
#include <QString>
#include <QAction>
#include <thread>
#include <QScrollBar>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //m_chartView = new QList<QWaveChartView*>();
    QObject::connect(ui->openAction,&QAction::triggered,this,&MainWindow::OpenFile);
    QObject::connect(ui->playAction,&QAction::triggered,this,&MainWindow::PlayData);
    for(int i=0;i<8;i++){
        m_chartView.append(new QWaveChartView());
        ui->verticalLayout->addWidget(m_chartView.at(i));
    }
    ui->scrollArea->verticalScrollBar()->installEventFilter(this);
}
void MainWindow::OpenFile()
{
    ushort data;
    QStringList filters;
    filters << "*.*" << "*.dat";
    QFileDialog* fdlg = new QFileDialog();
    fdlg->setNameFilters(filters);
    if(fdlg->exec() == QDialog::Accepted) {
        QString path = fdlg->selectedFiles()[0];
        QFile file(path);
        file.open(QIODevice::ReadOnly);
        QDataStream in(&file);

        long long times = 0;
        QList<QList<QPointF>> ptList;
        for(int i=0;i<8;i++)
            ptList.append(QList<QPointF>());
        while(!in.atEnd())
        {
            in >> data;
            ptList[times%8].append(QPointF(times/8,data));
            times++;

            if(times>800000)
                break;
        }

        for(int i=0;i<ptList.size();i++)
        {
            QList<QList<QPointF>> pts;
            pts.append(ptList.at(i));
            m_chartView.at(i)->paintLine(pts);
        }
    }
    else{

    }
}
bool MainWindow::eventFilter(QObject *target, QEvent *event)
{
    if(event->type()== QEvent::Wheel &&(((QWheelEvent*)event)->modifiers()&Qt::ControlModifier) != 0 ){
        event->ignore();
        return true;
    }else{
        return QWidget::eventFilter(target,event);
    }
}
void MainWindow::PlayData()
{

}
MainWindow::~MainWindow()
{
    delete ui;
}
